﻿
using StackExchange.Redis;
using SvGame.Objects;
using System.Data;
using UtilLib;

namespace SvGame.Controller;

public partial class GameHandler
{
    /// <summary>
    /// 战斗
    /// </summary>
    /// <param name="pack"></param>
    public int C2GFight(MyPackage pack)
    {
        pack.GetMsg(out C2GFight req);
        var rsp = new G2CFight();

        if (!pack.PackGetPlayer(out var player) || player is null)
        {
            return 0;
        }

        if (req.Uid == player.Puid)
        {
            return player.MessageError("你不能攻击自己");
        }
        if (!player.Room.CanFight())
        {
            return player.MessageError("这里禁止战斗。");
        }
        if (player.IsBusy)
        {
            return player.MessageError(BusyMessage);
        }

        if (player.Room.DictPlayer.TryGetValue(req.Uid, out var targetPlayer))
        {
            MessageVision($"$N对着$n说道：{QuerySelf(player)}{player.Name}，领教{QueryRespect(targetPlayer)}的高招！", 
                player, 
                targetPlayer);
            targetPlayer.Message($"如果你愿意和对方进行比试，请你也对{player.Name}发起比试", EMudColor.YEL);
            player.Message("由于对方是由玩家控制的人物，你必须等对方同意才能进行比试。", EMudColor.YEL);
            player.FightOb(targetPlayer);
        }
        else if(player.Room.DictNpc.TryGetValue(req.Uid, out var targetNpc))
        {
            if (!targetNpc.CanFight)
            {
                return player.MessageError($"{targetNpc.Cfg.SettBase.Name}对你摇了摇头。");
            }
            MessageVision($"$N对着$n说道：{QuerySelf(player)}{player.Name}，领教{QueryRespect(targetNpc)}的高招！",
                player,
                targetNpc);
            player.FightOb(targetNpc);
            targetNpc.FightOb(player);
        }
        else
        {
            return player.MessageError("你想和谁比试？");
        }

        return pack.Send(rsp);
    }
    /// <summary>
    /// 杀
    /// </summary>
    /// <param name="pack"></param>
    public int C2GKill(MyPackage pack)
    {
        pack.GetMsg(out C2GKill req);
        var rsp = new G2CKill();

        if (!pack.PackGetPlayer(out var player) || player is null)
        {
            return 0;
        }

        if (req.Uid == player.Puid)
        {
            return player.MessageError("你不能杀死自己");
        }
        if (!player.Room.CanFight())
        {
            return player.MessageError("这里禁止战斗。");
        }
        if (player.IsBusy)
        {
            return player.MessageError(BusyMessage);
        }

        if (player.Room.DictPlayer.TryGetValue(req.Uid, out var targetPlayer))
        {
            MessageVision($"$N对着$n喝道：「{QueryRude(targetPlayer)}！今日不是你死就是我活！」\n",
                player,
                targetPlayer);
            player.KillOb(targetPlayer);
            targetPlayer.Message($"如果你要和{player.Name}性命相搏，请你也对这个人下一次 杀 指令。", EMudColor.HIR);

            targetPlayer.FightOb(player);
        }
        else if (player.Room.DictNpc.TryGetValue(req.Uid, out var targetNpc))
        {
            if (!targetNpc.CanFight)
            {
                return player.MessageError($"{targetNpc.Cfg.SettBase.Name}对你摇了摇头。");
            }
            MessageVision($"$N对着$n喝道：「{QueryRude(targetNpc)}！今日不是你死就是我活！」\n",
                player,
                targetNpc);
            player.KillOb(targetNpc);
            targetNpc.KillOb(player);
        }
        else
        {
            return player.MessageError("这里没有这个人。");
        }

        return pack.Send(rsp);
    }
}
